<?php
require_once "config.php";
require_once 'class/class.phpmailer.php';
//require_once 'extranet_utils.php';
error_reporting(0);
/*require_once 'class/Member.php';
;*/
//print_r( $_SESSION["orderprocessarray"]);
//
//if()
 $offer_obj = new Offer();
 $member_obj = new Member();
 $orderid=$_GET['id'];
 $member_id=isset($_SESSION['member_id'])?$_SESSION['member_id']:0;
 if(!$offer_obj->is_valid_order($orderid, $member_id)){
     $err_msg="Sorry, you are not authorized to view this order details";
     header("Location:index.php");
     exit();
 }
 $summary=$offer_obj->get_order_details($orderid);
 //$orderid=$offer_obj->addofferorder($_SESSION["orderprocessarray"]);
 //$_SESSION["orderprocessarray"]["orderid"]=$orderid;
//print_r($_SESSION["orderprocessarray"]);
 if($_SESSION["orderprocessarray"]["isfreeoffer"]==1)   // || $_SESSION["orderprocessarray"]["offer_id"]==1
 {
     $offer_obj->updateorderstatus('C');
     $credits_redeemed=isset($_SESSION["orderprocessarray"]['credits_redeemed'])?$_SESSION["orderprocessarray"]['credits_redeemed']:0;
     
     if($credits_redeemed>0){
        $member_obj->update_member_credits($_SESSION["orderprocessarray"]['member_id'], $_SESSION["orderprocessarray"]['credits_redeemed'], '-');
        $_SESSION['pending_credit']=$_SESSION['pending_credit']-$credits_redeemed;        
     } 
     $ordercompleted=1;
 }
 else{
     //send to payment gateway
 }


 if($ordercompleted==1){
      $offer_obj->updateoffertable();
      //$member_obj=new Member();          
      $member_details=$member_obj->get_member_details($_SESSION["orderprocessarray"]['member_id']);
      //echo "<pre>";
      //print_r($_SESSION);exit();
      if(isset($_SESSION["orderprocessarray"]['invited_by']) && $member_details['invite_credited']=="N"){
          
          //update the new member
          $update_member_details=array("invite_credited"=>"Y"); //, "pending_credit"=>"coalesce(pending_credit, 0)+100"
          $member_obj->update_member($update_member_details, $_SESSION["orderprocessarray"]['member_id']);
          //udate the referrer details
          //add 100 credits to the existing 'pending_credit' colimn
          $member_obj->update_member_credits($_SESSION["orderprocessarray"]['invited_by']);
      }

       $email_message = "<img src='http://www.hotelswithoffers.com/images/logo.png' /><br/>";
            $email_message .= "Dear " . $to_name . ",<br/><br/>";
            $email_message .= "Thank you for Your Order . <br/>   <br/><br/>";
            $email_message .= "Regards <br/>";
            $email_message .= "hotelswithoffers   Team.\n";

            // To send HTML mail, the Content-type header must be set
       echo     $subject = 'Email Verification for your order at  hotelswithoffers.com Order Id:'. $_SESSION["orderprocessarray"]["orderid"];

            $mail = new PHPMailer();
		$mail->IsMail();
		$mail->IsHTML(true);
                $mail->FromName = 'Hotelswithoffers';
                $mail->From = 'hotelswithoffers.com';
		$mail->AddAddress($_SESSION["orderprocessarray"]["member_email"]);
                $mail->AddCC('induneth87@gmail.com');
		$mail->Subject = $subject;
		$mail->Body = $email_message;
		//print_r($mail->Send());
                 // header("Location:index.php") ;
		if(!$mail->Send())
		{
		   echo "Error sending: " . $mail->ErrorInfo;;
		}
                else{
                    $mail->ClearAddresses();
                    $mail->ClearAttachments();
                }
                unset ($_SESSION["orderprocessarray"]);
                //print_r($_SESSION["orderprocessarray"]);
 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hotels With Offers</title>
<script language="Javascript" type="text/javascript" src="js/jquery-1.4.1.js"></script>

<script src="SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />

<script language="javascript" type="text/javascript">
        function submitform(form)
        {
            form.submit();
        }
</script>
</head>

<body>
<div id="wrapper">
<?php require_once 'top_header.php'; ?>
    
<div class="contentPane">
  <div id="sidebar">
    <span class="menu"><img src="images/call.png" alt="Advertise with Hotel with Offers" class="callus"  /></span>
    <div class="joinus">
      <h1>INVITE A FRIEND</h1>
      <h2>Tell a friend and win Rs100.00</h2>
      <div class="joinbtn"><a href="#">LEARN&nbsp;MORE</a></div>
     </div>
    <div class="reserve"><span class="reserveitem">Reserved for :</span> <span class="reserveitem">advertising</span> <span class="reserveitem">latest news</span> <span class="reserveitem">soon ending deals &amp; offers</span></div>
  </div>
  <div class="leftPane">
      <div class="mainMenu"><?php require_once 'main_menu.php'; ?></div>
      <div id="Listings">
      <div class="listingdetails">
          
          <?php if(!empty($err_msg)):   //err_msg?>
          <span><?php echo $err_msg; ?></span>
          <?php else: ?>
          
<?php if( $ordercompleted==1):?>
<p> Thank you for your order! A confirmation mail is sent to your email! </p>
<?php
unset($ordercompleted);
endif;?>
<?php if(!empty($orderid)):?>
<table style="border: 1px solid rgb(204, 204, 204);">    
    <tr>        
        <td colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);">Order Number: <?php echo $summary['order_number'] ?></td>
        <td colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);">Order Date: <?php echo $summary['creation_date'] ?></td>
    </tr>
    <tr>
        <td colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);"><img src="/images/hotels/thumbs/<?php echo $summary['image_name'] ?>" width="120"/></td>
        <td colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['offer_title'] ?></td>
    </tr>
    <tr>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">No. of vouchers</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Regular price</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Offer price</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Credits redeemed</td>
    </tr>
    <tr>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['no_of_offer_vouchers'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['regular_amount'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['discount_amount'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['credits_redeemed'] ?></td>
    </tr>
    <tr>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Customer Name</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Phone</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Available credits</td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);">Email</td>
    </tr>
    <tr>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['first_name'] ?> <?php echo $summary['last_name'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['contact_number'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['pending_credit'] ?></td>
        <td style="border-bottom: 1px solid rgb(204, 204, 204);"><?php echo $summary['member_email'] ?></td>
    </tr>    
</table>
<?php endif;?>
<?php endif;    //err_msg?>
      </div>
      </div>
</div>
<div class="clear"></div>

</div>
</div>
<div class="clear"></div>

<div id="footerWrapper">
<?php require_once 'footer.php'; ?>
</div>
    <script language="javascript" type="text/javascript">
        var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1");
    </script>
</body>
</html>
